/*******************************************************************************
 * Copyright 2009-2015 Amazon Services. All Rights Reserved.
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 *
 * You may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at: http://aws.amazon.com/apache2.0
 * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 
 * specific language governing permissions and limitations under the License.
 *******************************************************************************
 * Marketplace Web Service Orders
 * API Version: 2013-09-01
 * Library Version: 2015-04-30
 * Generated: Thu May 28 20:14:49 GMT 2015
 */
package com.amazonservices.mws.orders._2013_09_01.samples;

import java.util.*;
import java.util.concurrent.*;
import javax.xml.datatype.XMLGregorianCalendar;
import java.math.BigDecimal;

import com.amazonservices.mws.client.*;
import com.amazonservices.mws.orders._2013_09_01.*;
import com.amazonservices.mws.orders._2013_09_01.model.*;

/** Sample async call for ListOrdersByNextToken. */
public class ListOrdersByNextTokenAsyncSample {

    /**
     * Call the service, log response and exceptions.
     *
     * @param client
     * @param request
     *
     * @return The response.
     */
    public static List<Object> invokeListOrdersByNextToken(
            MarketplaceWebServiceOrdersAsync client, 
            List<ListOrdersByNextTokenRequest> requestList) {
        // Call the service async.
        List<Future<ListOrdersByNextTokenResponse>> futureList = 
            new ArrayList<Future<ListOrdersByNextTokenResponse>>();
        for (ListOrdersByNextTokenRequest request : requestList) {
            Future<ListOrdersByNextTokenResponse> future = 
                client.listOrdersByNextTokenAsync(request);
            futureList.add(future);
        }
        List<Object> responseList = new ArrayList<Object>();
        for (Future<ListOrdersByNextTokenResponse> future : futureList) {
            Object xresponse;
            try {
                ListOrdersByNextTokenResponse response = future.get();
                ResponseHeaderMetadata rhmd = response.getResponseHeaderMetadata();
                // We recommend logging every the request id and timestamp of every call.
                System.out.println("Response:");
                System.out.println("RequestId: "+rhmd.getRequestId());
                System.out.println("Timestamp: "+rhmd.getTimestamp());
                String responseXml = response.toXML();
                System.out.println(responseXml);
                xresponse = response;
            } catch (ExecutionException ee) {
                Throwable cause = ee.getCause();
                if (cause instanceof MarketplaceWebServiceOrdersException) {
                    // Exception properties are important for diagnostics.
                    MarketplaceWebServiceOrdersException ex = 
                        (MarketplaceWebServiceOrdersException)cause;
                    ResponseHeaderMetadata rhmd = ex.getResponseHeaderMetadata();
                    System.out.println("Service Exception:");
                    System.out.println("RequestId: "+rhmd.getRequestId());
                    System.out.println("Timestamp: "+rhmd.getTimestamp());
                    System.out.println("Message: "+ex.getMessage());
                    System.out.println("StatusCode: "+ex.getStatusCode());
                    System.out.println("ErrorCode: "+ex.getErrorCode());
                    System.out.println("ErrorType: "+ex.getErrorType());
                    xresponse = ex;
                } else {
                    xresponse = cause;
                }
            } catch (Exception e) {
                xresponse = e;
            }
            responseList.add(xresponse);
        }
        return responseList;
    }

    /**
     *  Command line entry point.
     */
    public static void main(String[] args) {

        // Get a client connection.
        MarketplaceWebServiceOrdersAsyncClient client = MarketplaceWebServiceOrdersSampleConfig.getAsyncClient();

        // Create a request list.
        List<ListOrdersByNextTokenRequest> requestList = new ArrayList<ListOrdersByNextTokenRequest>();
        ListOrdersByNextTokenRequest request = new ListOrdersByNextTokenRequest();
        String sellerId = "A31FK1YOBVUT0K";
        request.setSellerId(sellerId);
        String mwsAuthToken = "example";
        request.setMWSAuthToken(mwsAuthToken);
        String nextToken = "pn6m/NogPxt/YYuZQbKv1QafEPREmauvizt1MIhPYZZJmEd9UfhpcXMXrhAERS3NukcmlfaYSVvNeGyEmOrLe7q8xqAchQNKtU38YwqRuVIwqcbUqh/LhCJ0wMvlylZkE3RvwDUQBA1G6ShfNgiExhKFL/RxM5pTRcAcYEgjGoxWL0SZZXGXWx35jNz11rwFT4mKBv/7QdWLpq54KmAdgf0IfGeAvb95J/gMIjfO756fpVJGS+vu5pFHMXy6XL5lMbGym30Zxtisc0X6TC+oUL+7LE2piFZMsJdJobvDtjYGtweTBI2dtZUzYV7MlAUoEHeeaI4WsQaok31kqFVwo79xcusGG5a1fwylPI5qZH51NMrpM18PB5GesRwkZOQCBLVDshZ93sHthhfBGj6DwpxMeJyv/sQd25news0zC1wb9NIwUAVD+fMWdZOJGcrbWR1xTfDBygqH86mPcGobNpdwVgjRe55fa6F4Z+6IvOASyoYkkmy7z4+envNcHi41mAP1jf9xS7/IWeUiwkeGtM9mNOdUz8cZ";
        request.setNextToken(nextToken);
        requestList.add(request);

        // Make the calls.
        ListOrdersByNextTokenAsyncSample.invokeListOrdersByNextToken(client, requestList);

    }

}
